//
//  NSObject+Tool.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/24.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "NSObject+Tool.h"

@implementation NSObject (Tool)

#pragma mark - 获取当前控制器
-  (UIViewController *)getCurrentVC {
    
    UIViewController* currentViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    
    BOOL runLoopFind = YES;
    while (runLoopFind) {
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
        } else if ([currentViewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
        } else if ([currentViewController isKindOfClass:[UITabBarController class]])
        {
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        } else
        {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
            if (childViewControllerCount > 0) {
                currentViewController = currentViewController.childViewControllers.lastObject;
                return currentViewController;
            } else {
                return currentViewController;
            }
        }
    }
    return currentViewController;
}

@end
